package com.industry.staff.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.industry.framework.common.Result;
import com.industry.framework.common.page.TableDataInfo;
import com.industry.framework.core.annountions.monitor.IdooMonitorService;
import com.industry.framework.core.generic.AbstractCoreGenericController;
import com.industry.framework.core.wrapper.QueryTableWrapper;
import com.industry.framework.core.wrapper.WrapperExtractor;
import com.industry.staff.controller.vo.DepartmentVo;
import com.industry.staff.dal.dataobject.Department;
import com.industry.staff.service.DepartmentService;
import jakarta.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author nbZhuozh
 * @Date 2025-04-10  15:58
 **/
@RestController
@RequestMapping("staff-department")
public class DepartmentController extends AbstractCoreGenericController<Department, String> {

    @Resource
    private DepartmentService departmentService;

    public Result<TableDataInfo<Department>> queryList(@RequestBody QueryTableWrapper<Department> queryTableWrapper) {
        WrapperExtractor<Department> wrapperExtractor = new WrapperExtractor<>(Department.class);
        //构造condition查询
        QueryWrapper<Department> wrapper = wrapperExtractor.queryWrapperBuilder(queryTableWrapper);
        //构造Page
        IPage<Department> iPage = getService().getMapper().selectPage(wrapperExtractor.excuteRowBounds(), wrapper);
        //这里需要汇总下当前部门下的所有子部门数量
        List<Department> records = iPage.getRecords();
        List<DepartmentVo> departmentTrees = new ArrayList<>();
        if(!CollectionUtils.isEmpty(records)){
            departmentTrees = departmentService.buildTree(records);
            for (Department s : records) {
                //递归 && 拿出一坨
                DepartmentVo departmentVo = DepartmentVo.calChildrenCountDFS(departmentTrees, s.getId());
                //递归一坨
                if(departmentVo != null){
                    Integer count = DepartmentVo.calChildrenCountDFS(departmentVo);
                    s.setDeptChildrenCount(String.valueOf(count));
                }
            }
        }

        return Result.success(TableDataInfo.build(iPage));
    }


    @IdooMonitorService("获取部门树")
    @GetMapping("query-tree")
    public Result<List<DepartmentVo>> queryDepartmentTree(){
        List<DepartmentVo> result = new ArrayList<>();
        List<Department> list = departmentService.getMapper().selectList(Wrappers.query());
        if(!CollectionUtils.isEmpty(list)){
            result = departmentService.buildTree(list);
        }
        return Result.success(result);

    }

}
